package com.app.jpush.controller;

import com.alibaba.fastjson.JSONObject;
import com.app.jpush.service.JPushService;
import com.app.login.service.LoginService;
import com.app.push.Utils.PushTypeEnum;
import com.app.push.controller.UPushController;
import com.app.sysmodule.reponse.JsonResult;
import com.app.sysmodule.utils.JsonData;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by song on 2018/1/22.
 */
@Controller
@RequestMapping(value = "/jpush")
public class JpushController {
    private static Logger log = org.apache.log4j.Logger.getLogger(UPushController.class);
    @Autowired
    private LoginService loginService;
    @Autowired
    private JPushService jPushService;
    @RequestMapping(value = "/sendMessage",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult sendMessage(HttpServletRequest request, HttpServletResponse response){
        JsonResult jsonResult= new JsonResult();
        JSONObject jsonObject = JsonData.getRequestBodyData(request);
        log.info("极光推送收到消息："+jsonObject);
        if(null != jsonObject){
            List<String> userList = (List<String>)jsonObject.get("userList");
            List<String> companyList = (List<String>)jsonObject.get("companyList");//公司列表
            String handleType = jsonObject.getString("handleType");
            String source = jsonObject.getString("source");
            String channel = (String)jsonObject.get("channel");//公司id
            String display_type = null;//预留
            String filter_type = jsonObject.getString("filterType");//01:通用过滤  02 自定义过滤
            String filter = jsonObject.getString("filter");
            String ticker = jsonObject.getString("ticker");
            String title = jsonObject.getString("title");
            String text = jsonObject.getString("text");
            String appSecret = jsonObject.getString("appSecret");
            try{
                //判断用户名密码
                boolean flag = loginService.verification(source,appSecret);
                if(flag){
                    if(StringUtils.isNotEmpty(source) && StringUtils.isNotEmpty(filter_type) && StringUtils.isNotEmpty(ticker) && StringUtils.isNotEmpty(title) && StringUtils.isNotEmpty(text) && StringUtils.isNotEmpty(handleType)){
                        JSONObject  result = jPushService.handleGroupcast(userList,companyList,channel,source,display_type,filter_type,filter,ticker,title,text,handleType);
                        if(null != result && null != result.get("statusCode") && ((Integer)result.get("statusCode"))==0){
                            jsonResult.setCode(0);
                            jsonResult.setMsg("请求成功");
                        }else {
                            jsonResult.setCode(1);
                            jsonResult.setMsg("请求失败，请联系管理员");
                        }
                        log.info("极光推送返回信息："+result);
                        jsonResult.setData(result);
                        return jsonResult;
                    }else {
                        jsonResult.setMsg("请求参数有误，请检查请求参数");
                        jsonResult.setData(jsonObject);
                        jsonResult.setCode(1);
                    }
                }else {
                    jsonResult.setMsg("用户名密码错误");
                    jsonResult.setCode(1);
                    return jsonResult;
                }
            }catch (Exception e){
                e.printStackTrace();
                log.error(e.getMessage());
                jsonResult.setMsg("推送消息失败,请联系管理员");
                jsonResult.setCode(1);
            }
        }
        return jsonResult;
    }

}
